
import java.util.List;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * esta es la clase Hero donde se controla todo lo que le pasa.
 * @author Paola valle Pazos y Sonia Estefania Monasterio Ochoa
 * @version noviembre.2014
 */
public class Hero extends Actor
{   
    private World mundo_aux;
    private int x ,y;
    private int vidas;
    private int puntos;
    private boolean espada;  
    private int nivel;
    
    private GreenfootImage []moveRight;
    private GreenfootImage []moveLeft;
    private GreenfootImage []moveUp;
    private GreenfootImage []moveDown;
    
    private GreenfootImage walkRight1 = new GreenfootImage("linkDer.png");
    private GreenfootImage walkRight2 = new GreenfootImage("linkDerPie.png");
    
    private GreenfootImage walkLeft1 = new GreenfootImage("linkIzq.png");
    private GreenfootImage walkLeft2 = new GreenfootImage("linkIzqPie.png");
    
    private GreenfootImage walkUp1 = new GreenfootImage("linkAtras.png");
    private GreenfootImage walkUp2 = new GreenfootImage("linkAtrasPieDer.png");
    private GreenfootImage walkUp3 = new GreenfootImage("linkAtrasPieIzq.png");
    
    private GreenfootImage walkDown1 = new GreenfootImage("linkFrente.png");
    private GreenfootImage walkDown2 = new GreenfootImage("linkFrentePieDer.png");
    private GreenfootImage walkDown3 = new GreenfootImage("linkFrentePieIzq.png");
    
    //cuando ya tiene la espada    
    private GreenfootImage espaRight = new GreenfootImage("DerEspada.png");
    private GreenfootImage espaLeft = new GreenfootImage("IzqEspada.png");
    private GreenfootImage espaUp = new GreenfootImage("AtrasEspada.png");
    private GreenfootImage espaDown = new GreenfootImage("FrenteEspada.png");
    
    private int contMoveRight;
    private int contMoveLeft;
    private int contMoveUp;
    private int contMoveDown;        
    
    //private SimpleTimer timer;// = new SimpleTimer();    
    //private Counter contTimer;
    
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Hero()
    {
        vidas = 3;
        puntos = 0;
        espada = false;
        nivel = 1;                        
                
        moveRight = new GreenfootImage[2];
        moveLeft = new GreenfootImage[2];
        moveUp = new GreenfootImage[3];
        moveDown = new GreenfootImage[3];
        
        moveRight[0] = walkRight1;
        moveRight[1] = walkRight2;
        
        moveLeft[0] = walkLeft1;
        moveLeft[1] = walkLeft2;
        
        moveUp[0] = walkUp1;
        moveUp[1] = walkUp2;
        moveUp[2] = walkUp3;
        
        moveDown[0] = walkDown1;
        moveDown[1] = walkDown2;
        moveDown[2] = walkDown3;
        
        contMoveRight = 0;
        contMoveLeft = 0;
        contMoveUp = 0;
        contMoveDown = 0;        
    }
    
    public void act() 
    {                 
        x =this.getX();
        y = this.getY();        
        
        mover();
        boolean val = malos();        

        if(this.malos() == true)
        {
            reinicia_pos(1);
        }
        modo_Asesino();
        
        if(vidas == 0)
        {
            GameOver gameOver;
            gameOver = new GameOver();
            Greenfoot.setWorld(gameOver);
            
            ScoreBoard score;
             getWorld().addObject(score=new ScoreBoard(600, 400), (getWorld().getWidth()) /2, (getWorld().getHeight()) /2);
             score.guardaRecord(puntos);
        }
        
        
        
        switch(nivel)
        {
            case 1:
            if((((Labyrinth1)this.getWorld()).controlTimer()) == 90)
            {
                GameOver gameOver;
                gameOver = new GameOver();
                Greenfoot.setWorld(gameOver);
            }
            
            case 2:
            if((((Labyrinth1)this.getWorld()).controlTimer()) == 60)
            {
                GameOver gameOver;
                gameOver = new GameOver();
                Greenfoot.setWorld(gameOver);
            }
            
            case 3:
            if((((Labyrinth1)this.getWorld()).controlTimer()) == 30)
            {
                GameOver gameOver;
                gameOver = new GameOver();
                Greenfoot.setWorld(gameOver);
            }
        }
    }
    
    public void mover()
    {
       int x = getX();
       int y = getY();
       int paso = 1;
       int nivel = 1;
        
       if(Greenfoot.isKeyDown("up"))
       {
           if(contMoveUp>2)
           {
               contMoveUp = 0;
            }
           
           this.setLocation(x, y - paso);
           this.setImage(moveUp[contMoveUp]);
           contMoveUp++;
       }
       
       if(Greenfoot.isKeyDown("down"))
       {
           if(contMoveDown>2)
           {
               contMoveDown = 0;
            }
           
           this.setLocation(x, y + paso);
           this.setImage(moveDown[contMoveDown]);
           contMoveDown++;
       }
       
       if(Greenfoot.isKeyDown("right"))
       {
           if(contMoveRight>1)
           {
            contMoveRight = 0;
           }
                       
           this.setLocation(x + paso, y);
           setImage(moveRight[contMoveRight]);
           contMoveRight++;
       }
       
       if(Greenfoot.isKeyDown("left"))
       {
           if(contMoveLeft>1)  
           {
               contMoveLeft = 0;
            }
            
           this.setLocation(x - paso, y);           
           this.setImage(moveLeft[contMoveLeft]);
           contMoveLeft++;
        }
       
       if(isTouching(WallV.class) || isTouching(WallH.class))
       {
           this.setLocation(x,y);
       }
       
       if(isTouching(Coin.class))
       {
           puntos++;
           this.removeTouching(Coin.class);           
        }
        
        if(isTouching(Heart.class))
        {
           vidas++;
           this.removeTouching(Heart.class);           
        }
        
        if(isTouching(MasterSword.class))
        {
           espada = true;
           this.removeTouching(MasterSword.class);
        }
        
        if(isTouching(Ghost.class))
        {            
            if(vidas > 0)
            {
                vidas--;
                 Labyrinth1 me_muero = (Labyrinth1)this.getWorld();
                 Counter vidas;
                 vidas = me_muero.getLifes();
                 vidas.setValue(vidas.getValue()-1);
                }
        } 
        
        if(isTouching(WallV.class) || isTouching(WallH.class))
        {
            this.setLocation(x,y);
       }    

        if(isTouching(Vampire.class))
        {            
                vidas = 0;                
        } 
        
        if(isTouching(Witch.class))
        {           
            if(puntos > 0)
                puntos--;                
        } 
                             
        if(isTouching(Door.class))// 
        {
           quita_Elementos();
           nivel++;
           Labyrinth1 aux = new Labyrinth1();
           aux = (Labyrinth1)this.getWorld();
           this.setLocation( 5, 54);
           aux.prepare(1);
           this.espada = false;
        }
                 
        if(isTouching(Door2.class))
        {
            quita_Elementos();
            nivel++;
            Labyrinth1 aux = new Labyrinth1();
            aux = (Labyrinth1)this.getWorld();
            this.setLocation( 5, 54);
            aux.prepare(2);
            this.espada = false;
        }       
         
         if(isTouching(Castle.class))
         {            
             ScoreBoard score;
             getWorld().addObject(score=new ScoreBoard(600, 400), (getWorld().getWidth()) /2, (getWorld().getHeight()) /2);
             score.guardaRecord(puntos);
             //((ScoreBoard)getWorld().getObjects(ScoreBoard.class).get(0)).guardaRecord(puntos);
             WindFinish zelda;           
             zelda = new WindFinish();                
             Greenfoot.setWorld(zelda);                          
         }                    
       }
       
       public void quita_Elementos()
       {
            List lista;            
            Labyrinth1 aux;
            aux = (Labyrinth1)this.getWorld();
            lista = aux.getObjects(WallV.class);
            aux.removeObjects(lista);
            lista =  aux.getObjects(WallH.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(Coin.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(Witch.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(Ghost.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(Vampire.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(Door.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(MasterSword.class);
            aux.removeObjects(lista);
            lista = aux.getObjects(Heart.class);
            aux.removeObjects(lista);
        }
        
       public boolean malos()
       {
            boolean band = false;
            if(this.isTouching(Witch.class) || this.isTouching(Vampire.class) || this.isTouching(Ghost.class))
            {
                band = true;
            }
            return band;
       }
       
       void reinicia_pos(int nivel)
       {
           switch(nivel)
           {
               case 1:
                this.setLocation( 5, 54);
               break;
            }
       }
       
       public void modo_Asesino()
       {
           if(espada == true)
            {
               if(Greenfoot.isKeyDown("up"))
               {
                   setImage("AtrasEspada.png");
                   this.setLocation(x, y - 1);
               }
           
               if(Greenfoot.isKeyDown("down"))
               {
                   setImage("FrenteEspada.png");
                   this.setLocation(x, y + 1);
               }
           
                if(Greenfoot.isKeyDown("right"))
               {
                  setImage("DerEspada.png");
                  this.setLocation(x + 1, y);
               }
           
              if(Greenfoot.isKeyDown("left"))
              {
                  setImage("IzqEspada.png");
                  this.setLocation(x - 1, y);   
              }
              
              if(isTouching(Witch.class))
              {            
                  removeTouching(Witch.class);
              } 
              
              if(isTouching(Ghost.class))
              {            
                  removeTouching(Ghost.class);
              }
              
              if(isTouching(Vampire.class))
              {            
                  removeTouching(Vampire.class);
              }
              
               if(isTouching(WallV.class) || isTouching(WallH.class))
               {
                   this.setLocation(x,y);
               }   
            }
       }
       
       public int regresaVidas()
       {
           return vidas;
        }
        
       public int regresaPuntos()
       {
           return puntos;           
        }
}                                                     